﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonHelpers;
using Sunny.UI;

namespace DevelopTool.Forms.SafetyForms
{
    /// <summary>
    /// 新用户窗体
    /// </summary>
    public partial class NewUserForm : BaseUIForm
    {
        /// <summary>
        /// 确认标志
        /// </summary>
        public int IsConfirm { get; set; } = 0;

        private LocalUserBll localUserBll;

        /// <summary>
        /// 拥有此窗体的父窗体
        /// </summary>
        private UIForm ownerForm;

        public NewUserForm(UIForm ownerForm)
        {
            InitializeComponent();
            this.ownerForm = ownerForm;
        }

        private void NewUserForm_Load(object sender, EventArgs e)
        {
            localUserBll = new LocalUserBll();

            uiToolTip.SetToolTip(btnOk, "一定要牢记密码啊~");
            uiToolTip.SetToolTip(btnLoging, "老同学吗~");

            Activate();
            textPassword1.Focus();

            this.ControlBoxFillHoverColor = this.ownerForm.ControlBoxFillHoverColor;

            ThemHelper.SetSystemThem(this);
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!base.CheckData(textPassword1,"请输入密码"))
            {
                textPassword1.Focus();
                return;
            }
            if (!base.CheckData(textPassword2,"请再次输入密码"))
            {
                textPassword2.Focus();
                return;
            }
            if (textPassword1.Text != textPassword2.Text)
            {
                this.ShowWarningNotifier("两次输入的密码不一致");
                return;
            }


            string msg = "1、请牢记密码，忘记密码将无法找回！\r" +
                         "2、请妥善保管密码，请勿将密码泄露给他人！\r" +
                         "3、将密码设置为不同于其它应用的密码，防撞库破解！\r" +
                         "4、密码将会加密，一旦遗失，理论上无法找回！\r" +
                         "5、若有疑问，请点击上方的【关于】按钮。";

            if (this.ShowAskDialog("重要提示", msg, UIStyle.Orange))
            {
                var tuple = localUserBll.NewUser(textPassword2.Text);
                if (tuple.Item1)
                {
                    IsConfirm = 1;
                }
                else
                {
                    this.ShowErrorDialog(tuple.Item2);
                }
            }
        }



        private void textPassword1_TextChanged(object sender, EventArgs e)
        {
            base.TextBoxTextChanged(sender, e);
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoging_Click(object sender, EventArgs e)
        {
            this.IsConfirm = 2;
        }

        private void NewUserForm_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                //绑定动画
                AnimatorHelper.BindShowAnimator(new List<Control>() {
                    { this.btnOk},
                });
            }
        }

        private void NewUserForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (IsConfirm == 0)
                base.ExitApp();
        }

        private void NewUserForm_ExtendBoxClick(object sender, EventArgs e)
        {
            AboutHelper.ShowAbout();
        }
    }
}
